<template>
  <v-chart :forceFit="true" :height="height" :data="data" :scale="scale" :onClick="handleClick" >
    <v-tooltip :showTitle="false" dataKey="item*percent"/>
    <v-axis/>
    <v-legend dataKey="item"/>
    <v-pie position="percent" color="item" :v-style="pieStyle" :label="labelConfig"/>
    <v-coord type="theta"/>
  </v-chart>
</template>

<script>
import { getTransactionPie } from '@api/api'

  const DataSet = require('@antv/data-set')
  import { ChartEventMixins } from './mixins/ChartMixins'
  import { USER_INFO } from '@/store/mutation-types'
  import Vue from 'vue'
  export default {
    name: 'Pie',
    mixins: [ChartEventMixins],
    props: {
      title: {
        type: String,
        default: ''
      },
      height: {
        type: Number,
        default: 254
      },
      dataSource: {
        type: Array,
        default: () => [
        ]
      }
    },
    data() {
      return {
        scale: [{
          dataKey: 'percent',
          min: 0,
          formatter: '.0%'
        }],
        pieStyle: {
          stroke: '#fff',
          lineWidth: 1
        },
        labelConfig: ['percent', {
          formatter: (val, item) => {
            return item.point.item + ': ' + val
          }
        }]
      }
    },
    computed: {
      data() {
        let dv = new DataSet.View().source(this.dataSource)
        // 计算数据百分比
        dv.transform({
          type: 'percent',
          field: 'count',
          dimension: 'item',
          as: 'percent'
        })
        return dv.rows
      }
    },
    created() {
      this.initLogInfo();
    },
    methods: {
      initLogInfo() {
        let userinfo = Vue.ls.get(USER_INFO);
        let params = {
          enterpriseUscc : userinfo.enterpriseUscc
        }
        getTransactionPie(params).then((res)=>{
          console.log(res)
          if(res){
            for(var i=0;i<res.result.length;i++){
              this.dataSource.push(res.result[i])
            }
          }
        })
        // let sellPie = [
        //   {count: 100000, item: '本集团（公司）'},
        //   {count: 1000000, item: '其他集团（公司）'}
        // ];
        // for (let i in sellPie) {
        //   this.dataSource.push(sellPie[i])
        // }

      },
    }
  }
</script>